package com.bytedy.sboot.api.controller;

import cn.hutool.json.JSONUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class BasicController {

    @GetMapping("/basic")
    public String getBasic() {
        log.info("get basic content.");
        return "basic content";
    }

    @RequestMapping(path = "/basic-throw", method = RequestMethod.GET)
    public String getThrow() {
        log.info("get basic-throw.");
        throw new RuntimeException("the.basic-throw.error");
    }


    @PreAuthorize("hasAnyAuthority('RA_admin')")
    @GetMapping("ts")
    public String ts() {
        Authentication authentication = SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication();
        log.info("auth: {}", JSONUtil.toJsonStr(authentication));
        authentication.getDetails();
        return "ts content.";
    }

}